package edu.uic.kott.fragments;

import java.util.List;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.activities.TrackKeyOutcomes;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Course;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TableLayout;

import android.widget.TextView;

public class TrackAttendance extends Fragment {

	//Representing focused date selected in Calendar Fragment
	static TextView classAttendanceOn;
	static String attendanceDate;
	public static Context attendanceContext;

	//Dynamic GUI holder
	private TableLayout attendanceQueryLayout;

	//Data
	KottDataSource datasource;
	List<Course> coursesList;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View track_attendance_view = inflater.inflate(R.layout.track_attendance, container, false);

		//Representing text view showing active date
		classAttendanceOn = (TextView) track_attendance_view.findViewById(R.id.classAttendanceOn);
		classAttendanceOn.setText(TrackKeyOutcomes.activeDate);

		attendanceQueryLayout = (TableLayout) track_attendance_view.findViewById(R.id.attendanceQueryLayout);

		//Returns to TrackKeyOutcomes that this fragment is visible
		TrackKeyOutcomes.attendanceVisible = true;
		TrackKeyOutcomes.UpdateVariables();

		AttendanceData();

		return track_attendance_view;
	}

	public void makeAttendanceGUI(long classId, String className, String courseNumber, String startTime, String instructor,
			String ta, String classLocation, int index) {
		LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View newRowView = inflater.inflate(R.layout.attendance_group_view, null);

		TextView classText = (TextView) newRowView.findViewById(R.id.classNameTextView);
		classText.setText(className);

		TextView courseText = (TextView) newRowView.findViewById(R.id.attendanceCourseNumber);
		courseText.setText(courseNumber);

		TextView timeText = (TextView) newRowView.findViewById(R.id.attendanceTimeOfClass);
		timeText.setText(startTime);

		TextView instructorText = (TextView) newRowView.findViewById(R.id.attendanceClassInstructor);
		instructorText.setText(instructor);

		TextView taText = (TextView) newRowView.findViewById(R.id.attendanceClassTA);
		taText.setText(ta);

		TextView locationText = (TextView) newRowView.findViewById(R.id.attendanceClassLocation);
		locationText.setText(classLocation);

		CheckBox attendedCheck = (CheckBox) newRowView.findViewById(R.id.attendanceAttendedCheckBox);
		attendedCheck.setOnCheckedChangeListener(attendedBoxListener);

		CheckBox cancelledCheck = (CheckBox) newRowView.findViewById(R.id.attendanceCancelledCheckBox);
		cancelledCheck.setOnCheckedChangeListener(cancelledBoxListener);

		TextView weekToDate = (TextView) newRowView.findViewById(R.id.attendanceWeekToDate);
		weekToDate.setText(getResources().getString(R.string.weekToDateAttendance) + " " + String.valueOf(WeekToDateAttendance(classId)));

		TextView termToDate = (TextView) newRowView.findViewById(R.id.attendanceTermToDate);
		termToDate.setText(getResources().getString(R.string.termToDateAttendance) + " " + String.valueOf(TermToDateAttendance(classId)));

		attendanceQueryLayout.addView(newRowView, index);
	}

	private long TermToDateAttendance(long id) {

		int scheduled = 9;
		int attended = 9;

		long percentage = attended/scheduled;

		return percentage;
	}

	private long WeekToDateAttendance(long id) {

		int scheduled = 3;
		int attended = 3;

		long percentage = attended/scheduled;


		return percentage;
	}

	public static void AttendanceUpdateOnDateChange() {
		//Updates active date to match date touched within Calendar fragment
		//Is called by TrackKeyOutcomes class
		attendanceDate = TrackKeyOutcomes.activeDate;
		classAttendanceOn.setText("On: " + attendanceDate);
	}

	@Override
	public void onStop() {
		TrackKeyOutcomes.attendanceVisible = false;
		super.onStop();
	}

	@Override
	public void onResume() {
		TrackKeyOutcomes.attendanceVisible = true;
		super.onResume();
	}

	public OnCheckedChangeListener attendedBoxListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			//Attended

		}
	};

	public OnCheckedChangeListener cancelledBoxListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			//Cancelled

		}
	};

	public void AttendanceData() {

		datasource = new KottDataSource(this.getActivity());

		//Run query to find courses on this date
		//coursesList = datasource.getCourseDetails();

		//Course course = new Course();


		//MAKE GUI
		/*for (Course c : coursesList) {

		long courseId = c.getCourseId();
		String courseName = c.getCourseName();
		String startTime = c.getStartTime();
		//String instructorName = c.getInstructor();
		//String taName = c.getTa();
		String classLocation = c.getClassLocation();

		makeAttendanceGUI(courseId, courseName, null, startTime, null, null, classLocation, 0);
	}*/

		//Above should end up looping like this
		makeAttendanceGUI(0 /*this is id*/, "Elementary Linear Algebra", "MATH120", "1:00", "Timothy Doyle", "This is a sample", "LCD 1", 0);
		makeAttendanceGUI(1, "Competitive Strategy", "IDS495", "3:30", "Mary Beth Watson-Manheim", "This is a sample 2", "Douglas Hall 230", 1);
	}

}
